<?php
namespace app\index\controller;

use \think\Db;
/**
 * 头部接口
 */
class Header extends \think\Controller
{
    //头部接口
    public function header()
    {
        try {
            $language = input("lang", "cn", "htmlspecialchars");
            $url = "http://www.genomicarebio.com";

            if ($language == "cn") {
                $menu_name = "menu_name_cn";
                $tab_name = "tab_name_cn";
                $list_title = "list_title_cn";
                $team_name = "team_name_cn";
            } elseif ($language == "en") {
                $menu_name = "menu_name_en";
                $tab_name = "tab_name_en";
                $list_title = "list_title_en";
                $team_name = "team_name_en";
            }
            $tab_name_const = "tab_name_cn";
            //系统名称
            $system_img = Db::query("select img_url from ls_system where `system_id`=1");

            //查询出menu所有数据除了p_id=0的
            $menu_data = Db::query("select menu_id,p_id,$menu_name as menu_name,menu_name_cn as menu_name_cn_if from ls_menu where `p_id`<>0 and `delete_time` is null order by sort ASC ");
            //查询tab
            $tab_data = Db::query("select tab_id,p_id,$tab_name as tab_name,tab_name_cn as tab_name_cn_if from ls_tab where `delete_time` is null");
            //循环拼接tab表内容

            foreach ($menu_data as $k => $v) {
                $menu_data[$k]['data_id'] = $v['menu_id'];
                //用户点击跳转连接
                if($v["p_id"]==1){
                        if($v["menu_name_cn_if"]=="领星产品"){
                        $tag = "fulltime";
                    }elseif($v["menu_name_cn_if"]=="领星学术"){
                        $tag = "academical";
                    }elseif ($v["menu_name_cn_if"]=="患者获益"){
                        $tag = "benefit";
                    }elseif ($v["menu_name_cn_if"]=="关于领星"){
                        $tag = "aboutus";
                    }
                    $menu_data[$k]['url'] = $url."/".$tag."?pageid=".$menu_data[$k]['data_id'];
                }else{
                    $tag = "kxy";
                    $menu_data[$k]['url'] = $url."/".$tag."?pageid=".$menu_data[$k]['data_id'];
                }
                $mmm = 1;
                foreach ($tab_data as $a => $b) {
                    if (isset($b["tab_name"]) && $b["tab_name"] == "临床试验&海外就医") {
                        $b["tab_name"] = "海外临床试验";
                    }
                    if ($v["menu_id"] == $b["p_id"] && $v["menu_name_cn_if"] !="铂金版") {
                        $b['url'] = $url."/".$tag."?tabid=".$mmm."&pageid=".$menu_data[$k]['data_id'];
                        $b['data_id'] = $v['menu_id'];
                        if (isset($menu_data[$k]["child"])) {
                            $menu_data[$k]["child"][] = $b;
                        } else {
                            $menu_data[$k]["child"] = [$b];
                        }
                        $mmm ++;
                    }

                }
            }
           // return ;

            //调用递归
            $menu = self::_get_child($menu_data, 1);
            //echo "<pre>";
            //print_r($menu);exit;
            //多癌种快速检测
            $can_id = Db::query("select tab_id from ls_tab where $tab_name_const='多癌种快速检测' and `delete_time` is null");
            $cancer = Db::query("select list_id,p_id,$list_title as list_title from ls_list where `p_id`={$can_id[0]['tab_id']} and `delete_time` is null order by manual_sort ASC");
            /*echo "<pre>";
            print_r($can_id);exit;*/
            //患者获益
            $ben_id = Db::query("select tab_id from ls_tab where $tab_name_const='患者获益' and `delete_time` is null");
            $benefit = Db::query("select list_id,p_id,$list_title as list_title from ls_list where `p_id`={$ben_id[0]['tab_id']} and `delete_time` is null order by manual_sort ASC ");

            //关于领星
            $about_id = Db::query("select tab_id from ls_tab where $tab_name_const='领星人' and `delete_time` is null");
            $team = Db::query("select team_id,tab_id,$team_name as team_name from ls_team where `tab_id`={$about_id[0]['tab_id']} and `delete_time` is null order by sort ASC ");
            $people_id = Db::query("select tab_id from ls_tab where $tab_name_const='领星伙伴' or $tab_name_const='领星实力' and `delete_time` is null");
            $list_p = Db::query("select list_id,p_id,$list_title as list_title from ls_list where `p_id`={$people_id[0]['tab_id']} or `p_id`={$people_id[1]['tab_id']} and `delete_time` is null order by manual_sort ASC ");
            //return json($list_p);exit;
            //添加多癌种快速检测and 患者获益list
            /*echo "<pre>";
            print_r($menu);exit;*/
            $arr = [];
            foreach ($menu as $k => $v) {
                if (isset($v["menu_name"]) && $v["menu_name_cn_if"] == "患者获益") {
                    $q =1;
                    $tag = "benefit";
                    foreach ($benefit as $k1 => $v1) {
                        $benefit[$k1]['data_id'] = $v['data_id'];
                        $benefit[$k1]["url"]=$url."/".$tag."?tabid=1"."&artid=stage".$q."&pageid=".$benefit[$k1]['data_id'];
                        $q ++;
                    }
                    $arr = $benefit;
                }
                if (isset($v["child"])) {
                    foreach ($v["child"] as $a => $b) {
                        // return json($b);exit;
                        //患者获益
                        if (isset($b["tab_name"]) && $b["tab_name_cn_if"] == "患者活动") {
                            $arr[] = $b;
                            $menu[$k]["child"] = $arr;
                        }
                        //康新源
                        if (isset($b["child"])) {
                            foreach ($b["child"] as $c => $d) {
                                //return json($d["$tab_name"]);exit;
                                if ($d["tab_name_cn_if"] == "多癌种快速检测") {
                                   $tabid =  substr($d["url"],0,strrpos($d["url"],'&'));
                                   $artid =  substr($tabid,-1);
                                    $nnn = 1;
                                    foreach ($cancer as $kk => $vv) {
                                        $cancer[$kk]['data_id'] = $b['data_id'];
                                        //多癌种检测链接
                                          $cancer[$kk]['url'] =$tabid ."&artid=tab".$artid."-stage".$nnn."&pageid=".$cancer[$kk]['data_id'];
                                        $nnn ++;
                                    }
                                    $menu[$k]["child"][$a]["child"][$c]["child"] = $cancer;

                                }
                            }
                        }
                        //领星人
                        if (isset($b["tab_name"]) && $b["tab_name_cn_if"] == "领星人") {
                            $purl =  substr($b["url"],0,strrpos($b["url"],'&'));
                            $r =1;
                            foreach ($team as $kk2 => $vv2) {
                                $team[$kk2]['data_id'] = $b['data_id'];
                                $team[$kk2]["url"] = $purl."&artid=team-".$r."&pageid=".$team[$kk2]['data_id'];
                                $r ++;
                            }
                            $menu[$k]["child"][$a]["child"] = $team;
                        }
                        //领星伙伴，领星实力
                        if (isset($b["tab_name"])) { //&&
                            $powerurl = substr($b["url"],0,strrpos($b["url"],'&'));
                            $y =  substr($powerurl,-1);
                            $x = 1;
                            foreach ($list_p as $e => $f) {
                                if($f["list_title"]=="City of Hope癌症研究与治疗中心"){
                                    $f["list_title"]="City of Hope";
                                }
                                //return json($f);exit;
                                if ($b["tab_id"] == $f["p_id"]) {
                                    // $list_p[$e]['data_id'] = $b['data_id'];
                                    //echo $b['data_id'], '<br/>';
                                    $f['data_id'] = $b['data_id'];
                                    $f["url"] = $powerurl."&artid=tab".$y."-stage".$x."&pageid=".$f['data_id'];
                                    $x ++;
                                    if (isset($menu[$k]["child"][$a]["child"])) {
                                        $menu[$k]["child"][$a]["child"][] = $f; //$list_p[$e];
                                    } else {
                                        $menu[$k]["child"][$a]["child"] = [$f];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //exit;
            /*echo "<pre>";
            print_r($menu);exit;*/
            return json([
                'code' => 200,
                'title_img'=>$system_img,
                'menu'=>$menu,
                "imsg" => "head头部名称"
            ]);
        }catch (\Exception $e){
		print_r($e);
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败'.$e->getMessage(),
            ]);
        }
    }

    //递归
    static protected function _get_child($data, $pid=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                //$v['child'] = self::_get_child($data, $v['menu_id']); //递归
                if (!array_key_exists('child', $v)) {//判断$v里面有没有child这个key
                    $v['child'] = self::_get_child($data, $v['menu_id']); //递归
                }
                $array[] = $v;
            }
        }
        return $array;
    }
}
?>